<!--
 * @Author: ydli
 * @LastEditTime: 2021-05-02 20:12:29
 * @Description: CoumnList
-->

<template>
  <div class="row">
    <div v-for="column in columnList" :key="column._id" class="col-4 mb-4">
      <div class="card shadow-sm h-100">
        <div class="card-body">
          <img
            :src="column.avatar && column.avatar.url"
            :alt="column.title"
            class="rounded-circle border border-light my-3"
            width="50"
            height="50"
          />
          <h5>{{ column.title }}</h5>
          <p class="card-text text-left">{{ column.description }}</p>
          <router-link
            :to="`/column/${column._id}`"
            class="btn btn-outline-primary mb-4"
            >进入专栏</router-link
          >
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, PropType, computed } from "vue";
import { ColumnProps } from "../store";

export default defineComponent({
  name: "CoumnList",
  props: {
    list: {
      // 想把构造函数断言成类型使用PropType
      type: Array as PropType<ColumnProps[]>,
      required: true,
    },
  },
  setup(props) {
    const columnList = computed(() => {
      return props.list.map((column) => {
        if (!column.avatar) {
          column.avatar = {
            url: require("@/assets/logo.png"),
          };
        } 
        return column;
      });
    });
    return {
      columnList,
    };
  },
});
</script>

